<?php
/**
* @name Maiji
* @author pd87@qq.com
* @copyright m-j.cc
* @version v1.0
*/
namespace MAIJI;
// 远程下载图片
class Httpimg
{
	public $_path = null;
	
	public function __construct(){
		$this -> _path = time();
    }
	
	public function downloadImage($url, $path='images/'){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
		$file = curl_exec($ch);
		curl_close($ch);
		return $this->saveAsImage($url, $file, $path);
	}
	
	private function saveAsImage($url, $file, $path){
		$filename = pathinfo($url, PATHINFO_BASENAME);
		$name = $filename . $this -> _path;
		$resource = fopen($path . $name, 'a');
		fwrite($resource, $file);
		fclose($resource);
		return $name;
	}
}